package com.tcwlkj.uuService.adapter;

import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.support.v7.widget.RecyclerView;
import android.text.Html;
import android.text.Spannable;
import android.text.SpannableStringBuilder;
import android.text.style.ForegroundColorSpan;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.tcwlkj.uuService.MyApp;
import com.tcwlkj.uuService.NetWork.HttpCallBack;
import com.tcwlkj.uuService.NetWork.RetrofitUtils;
import com.tcwlkj.uuService.R;
import com.tcwlkj.uuService.model.CommonResponse;
import com.tcwlkj.uuService.model.OrderModel;
import com.tcwlkj.uuService.model.UpdateMessage;
import com.tcwlkj.uuService.ui.NoOrderMapActivity;
import com.tcwlkj.uuService.utils.ChcheUtils;
import com.tcwlkj.uuService.utils.CommomDialog;
import com.tcwlkj.uuService.utils.EventCenter;
import com.tcwlkj.uuService.utils.MyDialog;
import com.tcwlkj.uuService.utils.PreferencesUtils;
import com.tcwlkj.uuService.utils.ToastUtil;
import com.tcwlkj.uuService.utils.Tools;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.List;

import retrofit2.Call;

/**
 * Created by Administrator on 2017/10/17 0017.
 */

public class NotSnathchAdapter extends RecyclerView.Adapter<NotSnathchAdapter.ViewHolder> {
    private List<OrderModel> modelList;
    private OrderModel snatchModel;
    private Context context;
    public NotSnathchAdapter(List<OrderModel> data, Context context) {
        this.modelList = data;
        this.context=context;
    }

    public void updateData(List<OrderModel> data) {
        this.modelList = data;
        notifyDataSetChanged();
    }

    @Override
    public NotSnathchAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        NotSnathchAdapter.ViewHolder viewHolder = new NotSnathchAdapter.ViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.recycler_item_no_order, parent, false));
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(NotSnathchAdapter.ViewHolder holder, int position) {
       // holder.setIsRecyclable(false);
        snatchModel = modelList.get(position);

        if(snatchModel.getHotBox().equals("0")){
            holder.tv_HotBox.setVisibility(View.GONE);
        }else {
            holder.tv_HotBox.setVisibility(View.VISIBLE);
        }
        switch (snatchModel.getOrderType()){
            case "0"://实时
                holder.tv_OrderType.setText("实时订单");
                holder.tv_time.setText("尽快送达");
                break;
            case "1"://预约
                holder.tv_OrderType.setText("预约订单");
                String str="预约时间：<font color='#41b1fb'>"+snatchModel.getSubscribeTime()+"</font>";
                holder.tv_time.setText(Html.fromHtml(str));
                break;
            case "2"://指派
                holder.tv_OrderType.setText("指派订单");
                holder.tv_time.setText("指派时间");
                break;
        }
        switch (snatchModel.getSendType()){
            case "10"://送
                holder.tv_qu.setText("取：");
                holder.tv_song.setText("送：");
                holder.tv_order_type.setText("同城送");
                holder.tv_endAdress.setText(snatchModel.getEndShowAddress());
                holder.tv_line.setVisibility(View.VISIBLE);
                holder.ll_start_distance.setVisibility(View.VISIBLE);
                holder.tv_end_distance.setVisibility(View.VISIBLE);
                holder.ll_paidui.setVisibility(View.GONE);
                holder.ll_goods.setVisibility(View.GONE);
                holder.tv_addmoney.setText(snatchModel.getSysAddMoney());
                holder.tv_goods_name.setText("物品类型：");
                break;
            case "20"://取
                holder.tv_qu.setText("取：");
                holder.tv_song.setText("收：");
                holder.tv_order_type.setText("同城取");
                holder.tv_endAdress.setText(snatchModel.getEndShowAddress());
                holder.tv_line.setVisibility(View.VISIBLE);
                holder.ll_start_distance.setVisibility(View.VISIBLE);
                holder.tv_end_distance.setVisibility(View.VISIBLE);
                holder.ll_paidui.setVisibility(View.GONE);
                holder.ll_goods.setVisibility(View.GONE);
                holder.tv_addmoney.setText(snatchModel.getSysAddMoney());
                holder.tv_goods_name.setText("物品类型：");
                break;
            case "30"://买
                holder.tv_qu.setText("买：");
                holder.tv_song.setText("收：");
                holder.tv_order_type.setText("同城买");
                holder.tv_endAdress.setText(snatchModel.getEndShowAddress());
                holder.tv_line.setVisibility(View.VISIBLE);
                holder.ll_start_distance.setVisibility(View.VISIBLE);
                holder.tv_end_distance.setVisibility(View.VISIBLE);
                holder.ll_paidui.setVisibility(View.GONE);
                holder.tv_addmoney.setText(snatchModel.getSysAddMoney());
                holder.ll_goods.setVisibility(View.VISIBLE);
                if(snatchModel.getPayGoodsMoney().equals("0.00")){
                    holder.tv_goods_money.setText("以小票为准");
                }else {
                    holder.tv_goods_money.setText(snatchModel.getPayGoodsMoney()+"元");
                }
                holder.tv_goods_name.setText("物品类型：");
                break;
            case "40"://帮
                holder.tv_qu.setText("帮忙地点:");
                holder.tv_song.setText("帮忙信息:");
                holder.tv_order_type.setText("同城帮");
                if(snatchModel.getHelpInfo().equals("")){
                    holder.tv_endAdress.setText("暂无");
                }else {
                    holder.tv_endAdress.setText(snatchModel.getHelpInfo());
                }
                holder.ll_paidui.setVisibility(View.GONE);
                holder.tv_end_distance.setVisibility(View.GONE);
                holder.ll_goods.setVisibility(View.GONE);
                holder.tv_addmoney.setText(snatchModel.getSysAddMoney());
                holder.tv_goods_name.setText("帮忙类型：");
                break;
            case "50"://排
                holder.tv_qu.setText("排队地点:");
                holder.tv_song.setText("排队信息:");
                holder.tv_order_type.setText("同城排");
                if(snatchModel.getHelpInfo().equals("")){
                    holder.tv_endAdress.setText("暂无");
                }else {
                    holder.tv_endAdress.setText(snatchModel.getHelpInfo());
                }
                holder.tv_paidui_time.setText(snatchModel.getExTimeSpan()+"分钟");
                holder.ll_paidui.setVisibility(View.VISIBLE);
                holder.tv_end_distance.setVisibility(View.GONE);
                holder.ll_goods.setVisibility(View.GONE);
                holder.tv_addmoney.setText(snatchModel.getSysAddMoney());
                holder.tv_goods_name.setText("排队类型：");
                break;
        }
        holder.tv_goods_type.setText(snatchModel.getQuickOpers());
        holder.tv_totalmoney.setText("￥"+snatchModel.getDriverFreightMoney());
        if (Double.valueOf(snatchModel.getDTakeDist()) < 1000) {
            holder.tv_DTakeDist.setText(Math.ceil(Double.valueOf(snatchModel.getDTakeDist())) + "");
            holder.tv_MorKM.setText("米");
        } else {
            holder.tv_DTakeDist.setText(new java.text.DecimalFormat("#.00").format(Double.valueOf(snatchModel.getDTakeDist()) / 1000));
            holder.tv_MorKM.setText("公里");
        }
        if(snatchModel.getSendSubType().equals("301")){//就近买
            holder.tv_begAdress.setText("就近买(不限制购买地址)");
        }else {
            holder.tv_begAdress.setText(snatchModel.getBegShowAddress());
        }


        if (Double.valueOf(snatchModel.getDTakeDist()) < 1000) {
            holder.tv_beg_distance.setText(Math.ceil(Double.valueOf(snatchModel.getDTakeDist())) + "m");
        } else {
            holder.tv_beg_distance.setText(new java.text.DecimalFormat("#.00").format(Double.valueOf(snatchModel.getDTakeDist()) / 1000) + "km");
        }
        holder.tv_end_distance.setText(snatchModel.getDistance());

        if(snatchModel.getPayUserFeeMoney().equals("0.00元")){
            holder.ll_feemoney.setVisibility(View.GONE);
        }else {
            holder.ll_feemoney.setVisibility(View.VISIBLE);
            holder.tv_feemoney.setText(snatchModel.getPayUserFeeMoney());
        }
        if(snatchModel.getSysAddMoney().equals("0.00元")){
            holder.ll_addmoney.setVisibility(View.GONE);
        }else {
            holder.ll_addmoney.setVisibility(View.VISIBLE);
            holder.tv_addmoney.setText(snatchModel.getSysAddMoney());
        }

        if(modelList.get(position).getRemarks().equals("")){
            holder.ll_beizhu.setVisibility(View.GONE);
        }else {
            holder.ll_beizhu.setVisibility(View.VISIBLE);
            holder.tv_remark.setText(snatchModel.getRemarks());
        }

        holder.btn_qiangdan.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(PreferencesUtils.getInt(MyApp.getContext(),PreferencesUtils.getString(MyApp.getContext(), "userID")+"work",5)==5){//收工
                    new CommomDialog(context, R.style.dialog, "您还未开工，无法抢单", new CommomDialog.OnCloseListener() {
                        @Override
                        public void onClick(Dialog dialog, boolean confirm) {
                        }
                    }).show();
                }else {
                    new CommomDialog(context, R.style.dialog, "确定要抢单吗?", new CommomDialog.OnCloseListener() {
                        @Override
                        public void onClick(Dialog dialog, boolean confirm) {
                            grab(modelList.get(position).getID());
                        }
                    }).show();
                }
            }
        });
        holder.ll_map.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                EventBus.getDefault().post(new EventCenter<OrderModel>(1003,modelList.get(position)));
            }
        });
    }
    private void grab(String orderId) {//抢单
        Call<CommonResponse> httpResultCall = RetrofitUtils.getRequest(context, true).grab(orderId, PreferencesUtils.getString(MyApp.getContext(), "userID"), 0, Tools.javabeanToJson(ChcheUtils.getInstance().getLocationModel()));
        httpResultCall.enqueue(new HttpCallBack<CommonResponse>() {
            @Override
            public void onSuccess(Object t) {
                CommonResponse commonResponse = (CommonResponse) t;
                EventBus.getDefault().post(new EventCenter<String>(111, commonResponse.getMessage()));
            }
            @Override
            public void onFail(int code, String message) {
                if (code == 40104) {
                    grab(orderId);
                } else {
                    ToastUtil.getInstance().show(message);
                }
            }
        });
    }

    @Override
    public int getItemCount() {
        return modelList.size();
    }

    public static class ViewHolder extends RecyclerView.ViewHolder {
        TextView tv_totalmoney;
        TextView tv_DTakeDist;
        TextView tv_OrderType;
        TextView tv_time;
        TextView tv_qu;
        TextView tv_song;
        TextView tv_begAdress;
        TextView tv_beg_distance;
        TextView tv_endAdress;
        TextView tv_end_distance;
        TextView tv_feemoney;
        TextView tv_addmoney;
        TextView tv_MorKM;
        Button btn_qiangdan;
        TextView tv_order_type;
        LinearLayout ll_start_distance;
        TextView tv_remark;
        TextView tv_line;
        LinearLayout ll_paidui;
        TextView tv_paidui_time;
        LinearLayout ll_map;
        TextView tv_HotBox;
        TextView tv_goods_type;
        TextView tv_goods_money;
        LinearLayout ll_goods;
        LinearLayout ll_addmoney;
        LinearLayout ll_feemoney;
        TextView tv_goods_name;

        LinearLayout ll_beizhu;

        public ViewHolder(View view) {
            super(view);
            tv_totalmoney = (TextView) view.findViewById(R.id.tv_totalmoney);
            tv_DTakeDist = (TextView) view.findViewById(R.id.tv_DTakeDist);
            tv_OrderType = (TextView) view.findViewById(R.id.tv_OrderType);
            tv_time = (TextView) view.findViewById(R.id.tv_time);
            tv_qu = (TextView) view.findViewById(R.id.tv_qu);
            tv_song = (TextView) view.findViewById(R.id.tv_song);
            tv_begAdress = (TextView) view.findViewById(R.id.tv_begAdress);
            tv_beg_distance = (TextView) view.findViewById(R.id.tv_beg_distance);
            tv_endAdress = (TextView) view.findViewById(R.id.tv_endAdress);
            tv_end_distance = (TextView) view.findViewById(R.id.tv_end_distance);
            tv_feemoney = (TextView) view.findViewById(R.id.tv_feemoney);
            tv_addmoney = (TextView) view.findViewById(R.id.tv_addmoney);
            tv_MorKM = (TextView) view.findViewById(R.id.tv_MorKM);
            btn_qiangdan = (Button) view.findViewById(R.id.btn_qiangdan);
            tv_order_type= (TextView) view.findViewById(R.id.tv_order_type);
            ll_start_distance= (LinearLayout) view.findViewById(R.id.ll_start_distance);
            tv_remark= (TextView) view.findViewById(R.id.tv_remark);
            tv_line= (TextView) view.findViewById(R.id.tv_line);
            ll_paidui= (LinearLayout) view.findViewById(R.id.ll_paidui);
            tv_paidui_time= (TextView) view.findViewById(R.id.tv_paidui_time);
            ll_map= (LinearLayout) view.findViewById(R.id.ll_map);
            tv_HotBox= (TextView) view.findViewById(R.id.tv_HotBox);
            tv_goods_type= (TextView) view.findViewById(R.id.tv_goods_type);
            tv_goods_money= (TextView) view.findViewById(R.id.tv_goods_money);
            ll_goods= (LinearLayout) view.findViewById(R.id.ll_goods);
            ll_addmoney= (LinearLayout) view.findViewById(R.id.ll_addmoney);
            ll_feemoney= (LinearLayout) view.findViewById(R.id.ll_feemoney);
            tv_goods_name= (TextView) view.findViewById(R.id.tv_goods_name);
            ll_beizhu= (LinearLayout) view.findViewById(R.id.ll_beizhu);
        }
    }

}
